package cn.edu.shengda.library;
//https://www.jianshu.com/p/212752f26a19
//db4free.net
//wangtianshun
//1234abcd
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login extends JFrame implements ActionListener{
    //按钮
    JButton btnLogin,btnRegister,btnCancel;
    //创建中间容器
    JPanel pnlSouth,pnlNorth,pnlCenter,pnlCenter1,pnlCenter2;
    //标签
    JLabel lbl1,JLabelNum,JLabelPwd;
    //用户名文本框
    JTextField tfNum;
    //密码文本框
    JPasswordField tfPwd;
    //创建窗口
    Login(String title){
        super(title);

        //north
        pnlNorth = new JPanel();
        lbl1 = new JLabel("欢迎进入图书管理系统！");
        pnlNorth.add(lbl1);
        this.add(pnlNorth,BorderLayout.NORTH);

        //center
        pnlCenter=new JPanel();
        pnlCenter1=new JPanel();
        pnlCenter2=new JPanel();
        pnlCenter.setLayout(new BorderLayout());
        JLabelNum=new JLabel("学   号：");
        tfNum=new JTextField(15);
        pnlCenter1.add(JLabelNum);
        pnlCenter1.add(tfNum);
        pnlCenter.add(pnlCenter1,BorderLayout.NORTH);
        JLabelPwd=new JLabel("密    码：");
        tfPwd=new JPasswordField(15);
        pnlCenter2.add(JLabelPwd);
        pnlCenter2.add(tfPwd);
        pnlCenter.add(pnlCenter2,BorderLayout.SOUTH);
        this.add(pnlCenter,BorderLayout.CENTER);

        //south
        pnlSouth = new JPanel();

        //生成按钮
        btnLogin = new JButton("登录");
        btnLogin.addActionListener(this);

        btnRegister = new JButton("注册");
        btnRegister.addActionListener(this);

        btnCancel = new JButton("取消");
        btnCancel.addActionListener(this);

        //将三个按钮放在一个中间容器中
        pnlSouth.add(btnLogin);
        pnlSouth.add(btnRegister);
        pnlSouth.add(btnCancel);
        //将按钮添加到图形界面
        //this.add(btnLogin);
        //this.add(btnRegister);
        //this.add(btnCancel);
        this.add(pnlSouth,BorderLayout.SOUTH);

        this.setSize(400, 180);
        //GUIUtil.toCenter(this);//使窗口居中
        this.setVisible(true);//可视化
        this.setResizable(false);//关闭放大窗口
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置错误关闭操作

    }
    @Override
    public void actionPerformed(ActionEvent e) {
        String name=this.tfNum.getText().trim();
        String pwd=this.tfPwd.getText().trim();
        if(e.getSource()==this.btnLogin){
            try {
                Class.forName("com.mysql.jdbc.Driver");
                String url="jdbc:mysql://172.28.22.100:3306/2017jsj2?characterEncoding=utf-8";
                String username="2017jsj2";
                String password="123abc";
                Connection conn=DriverManager.getConnection(url, username, password);
                Statement sta=conn.createStatement();
                //判定是否有此用户
                String sql="select * from user201705050201 where name='"+ name +"'";
                ResultSet rs=null;
                rs=sta.executeQuery(sql);
                int count=0;
                while(rs.next()){
                    count++;
                }
                if(count==0){
                    System.out.println("此用户不存在");
                }else{
                    int count2=0;
                    String sql2="select * from user201705050201 where name='"
                            +name+"' and pwd="+pwd;
                    rs=sta.executeQuery(sql2);
                    while(rs.next()){
                        count2++;
                    }
                    if(count2==0){
                        System.out.println("密码错误");
                    }else{
                        System.out.println("用户登录成功");
                    }
                }
                if(rs!=null){
                    rs.close();
                }
                if(sta!=null){
                    sta.close();
                }
                if(conn!=null){
                    conn.close();
                }
            } catch (ClassNotFoundException ex) {
            } catch (SQLException ex) {
            }finally{
            }
        }
        else if(e.getSource()==btnRegister){
            dispose();//关闭登录页面，跳到注册页面
            new Register("用户注册");
        }else {
            JOptionPane.showMessageDialog(this,"谢谢使用，欢迎下次再次使用本系统！");
            System.exit(0);
        }

    }
    public static void main(String[] args){
        Login login=new Login("图书管理系统登录界面");
    }
}
